/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ManagedServerListenerTester.cpp. Created on: 18.09.2009 15:30:10
 */

#include "ManagedServerListenerTester.hpp"

ManagedServerListenerTester::ManagedServerListenerTester() {
	reset();
}

ManagedServerListenerTester::~ManagedServerListenerTester() {
	notifyWaiter();
	notifyWaiterPerHandler();
	reset();
}

void ManagedServerListenerTester::onConnected(IManagedConnection* poConnection) throw() {
	boost::recursive_mutex::scoped_lock oScopedLock(m_mxConnections);
	poConnection->addRef();
	m_aConnections.push_back( poConnection );
	poConnection->addConnectionListener ( &m_oConnectionListener );
	poConnection->addStreamListener ( &m_oConnectionListener );

	// Add notification event
	this->m_bAccepted = true;
	this->notifyWaiter();

}

void ManagedServerListenerTester::onConnected(ConnectionHandler* poHandler) throw() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	addLastHandler(poHandler);
	notifyWaiterPerHandler();
}

void ManagedServerListenerTester::reset() {
	m_nLastErrorCode = 0;
	m_bAccepted = false;

	closeConnections();
	foreach(ConnectionHandler* poHandler, m_aHandlers) {
		poHandler->release();
	}
	m_aHandlers.clear();
}

bool ManagedServerListenerTester::isAccepted() {
	return m_bAccepted;
}

boost::recursive_mutex& ManagedServerListenerTester::getMonitor() {
	return m_mxMonitor;
}

boost::recursive_mutex& ManagedServerListenerTester::getMonitorPerHandler() {
	return m_mxMonitorPerHandler;
}

void ManagedServerListenerTester::waitForAnyEvent(boost::recursive_mutex::scoped_lock& oLock, const std::string& sLocation /*= "Unknown location"*/) {
	boost::xtime oXt;
	boost::xtime_get(&oXt, boost::TIME_UTC);
	oXt.sec += 10; // 10 sec is max await time
	if(!m_evAwait.timed_wait(oLock,oXt)) {
		std::cout << "Timeout for waiting accept event! Location: " << sLocation <<std::endl;
		throw SystemException("Timeout for waiting accept event! Location: " + sLocation);
	}
}

void ManagedServerListenerTester::waitForAnyEventPerHandler(boost::recursive_mutex::scoped_lock& oLock, const std::string& sLocation /*= "Unknown location"*/) {
	boost::xtime oXt;
	boost::xtime_get(&oXt, boost::TIME_UTC);
	oXt.sec += 10; // 10 sec is max await time
	if(!m_evAwait.timed_wait(oLock,oXt)) {
		std::cout << "Timeout for waiting accept event! Location: " << sLocation <<std::endl;
		throw SystemException("Timeout for waiting accept event! Location: " + sLocation);
	}
}


void ManagedServerListenerTester::notifyWaiter() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitor);
	m_evAwait.notify_one();
}

void ManagedServerListenerTester::notifyWaiterPerHandler() {
	boost::recursive_mutex::scoped_lock oLock(m_mxMonitorPerHandler);
	m_evAwaitPerHandler.notify_one();
}


void ManagedServerListenerTester::closeConnections(){

	// Close all connections
	boost::recursive_mutex::scoped_lock oScopedLock(m_mxConnections);
	foreach ( IManagedConnection* poConnection, m_aConnections ) {
		poConnection->release();
	}
	m_aConnections.clear();
}

ManagedServerListenerTester::Connections& ManagedServerListenerTester::getConnections(){
	return this->m_aConnections;
}

ManagedConnectionListenerTester& ManagedServerListenerTester::getConnectionListener() {
	return m_oConnectionListener;
}

ManagedServerListenerTester::Handlers& ManagedServerListenerTester::getLastHandlers() {
	return m_aHandlers;
}

void ManagedServerListenerTester::addLastHandler(ConnectionHandler *poHandler) {
	poHandler->addRef();
	m_aHandlers.push_back(poHandler);
}
